package com.cts.tmall.biz.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cts.tmall.dal.entity.TmallExpressRule;
import com.cts.tmall.dal.entity.TmallExpressTmp;
import com.cts.tmall.dal.mapper.TmallExpressRuleMapper;
import com.cts.tmall.dal.mapper.TmallExpressTmpMapper;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ExpressService {

    @Autowired
    private TmallExpressRuleMapper expressRuleMapper;

    @Autowired
    private TmallExpressTmpMapper expressTmpMapper;

    public TmallExpressTmp addTmp(TmallExpressTmp tmp) {
        expressTmpMapper.insert(tmp);
        return tmp;
    }

    public Map<String, Object> queryRule(Long createUserId) {
        TmallExpressRule expressRule = expressRuleMapper.selectOne(Wrappers.<TmallExpressRule>lambdaQuery()
                .eq(TmallExpressRule::getCreateUserId, createUserId));
        return ImmutableMap.<String, Object>builder()
                .put("multiPrdRule", expressRule == null ? "" : expressRule.getMultiPrdRule())
                .put("defaultTmpNo", expressRule == null ? "" : expressRule.getDefaultTmpNo())
                .build();
    }

    public void mergeRule(TmallExpressRule rule) {
        TmallExpressRule expressRule = expressRuleMapper.selectOne(Wrappers.<TmallExpressRule>lambdaQuery()
                .eq(TmallExpressRule::getCreateUserId, rule.getCreateUserId()));
        if (expressRule == null) {
            expressRule = new TmallExpressRule();
            expressRule.setCreateUserId(rule.getCreateUserId());
            expressRule.setMultiPrdRule(rule.getMultiPrdRule());
            expressRule.setDefaultTmpNo(rule.getDefaultTmpNo());
            expressRuleMapper.insert(expressRule);
        } else {
            expressRule.setMultiPrdRule(StringUtils.isNotBlank(rule.getMultiPrdRule()) ? rule.getMultiPrdRule() : expressRule.getMultiPrdRule());
            expressRule.setDefaultTmpNo(StringUtils.isNotBlank(rule.getDefaultTmpNo()) ? rule.getDefaultTmpNo() : expressRule.getDefaultTmpNo());
            expressRuleMapper.update(expressRule, Wrappers.<TmallExpressRule>lambdaQuery()
                    .eq(TmallExpressRule::getCreateUserId, rule.getCreateUserId()));
        }
    }

    public List<Map<String, Object>> queryTmp(Long createUserId) {
        List<TmallExpressTmp> tmallExpressTmps = expressTmpMapper.selectList(Wrappers.<TmallExpressTmp>lambdaQuery()
                .eq(TmallExpressTmp::getCreateUserId, createUserId));
        return tmallExpressTmps.stream().map(tmp ->
                ImmutableMap.<String, Object>builder()
                        .put("tmpNo", tmp.getTmpNo())
                        .put("tmpName", tmp.getTmpName())
                        .put("sendArea", StringUtils.isNotBlank(tmp.getSendArea()) ? tmp.getSendArea().split(",") : new String[0])
                        .put("sendPrice", tmp.getSendPrice().toPlainString())
                        .put("sendUnit", tmp.getSendUnit())
                        .put("defCount", tmp.getDefCount())
                        .put("defPrice", tmp.getDefPrice().toPlainString())
                        .put("unitCount", tmp.getUnitCount())
                        .put("unitPrice", tmp.getUnitPrice().toPlainString())
                        .put("targetArea", StringUtils.isNotBlank(tmp.getTargetArea()) ? tmp.getTargetArea().split(",") : new String[0])
                        .put("limitArea", StringUtils.isNotBlank(tmp.getLimitArea()) ? tmp.getLimitArea().split(",") : new String[0])
                        .put("freeArea", StringUtils.isNotBlank(tmp.getFreeArea()) ? tmp.getFreeArea().split(",") : new String[0])
                        .build()
        ).collect(Collectors.toList());
    }

    public void deleteTmp(Long tmpNo) {
        expressTmpMapper.deleteById(tmpNo);
    }
}
